﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using Microsoft.Win32;

namespace WPFMetronom
{
	public partial class MainWindow
	{
		#region Обработка кнопок

		private void radioButtonSimple_Checked ( object sender, RoutedEventArgs e )
		{
			if ( mbFormLoad )
			{
				mbSimpleOrExtend = true;
				ShowSimple ( );
			}
		}

		private void radioButtonExtended_Checked ( object sender, RoutedEventArgs e )
		{
			if ( mbFormLoad )
			{
				mbSimpleOrExtend = false;
				ShowExtend ( );
			}
		}

		private void buttonStepPart_Click ( object sender, RoutedEventArgs e )
		{
			if ( mbStepOrPart )
			{
				buttonStepPart.Content = "Часть";
				mbStepOrPart = false;
			}
			else
			{
				buttonStepPart.Content = "Шаг";
				mbStepOrPart = true;
			}
		}

		private void buttonTimeHit_Click ( object sender, RoutedEventArgs e )
		{
			if ( mbTimeOrHit )
			{
				buttonTimeHit.Content = "Удар";
				mbTimeOrHit = false;
			}
			else
			{
				buttonTimeHit.Content = "Время";
				mbTimeOrHit = true;
			}
		}

		private void buttonRepeatLoop_Click ( object sender, RoutedEventArgs e )
		{
			if ( mbRepeatOrLoop )
			{
				buttonRepeatLoop.Content = "Зациклить";
				sliderRepeatLoop.IsEnabled = false;
				mbRepeatOrLoop = false;
			}
			else
			{
				buttonRepeatLoop.Content = "Повторить";
				sliderRepeatLoop.IsEnabled = true;
				mbRepeatOrLoop = true;
			}
		}

		private void buttonStart_Click ( object sender, RoutedEventArgs e )
		{
			try
			{
				if ( !mbPlayButtonActive )
				{
					mbPlayButtonActive = mbPlaying = true;
					buttonStart.Content = "Стоп";

					moPresetObj = new PresetObj ( this );

					HideAll ( );

					Thread oPlayThread = new Thread ( new ThreadStart ( delegate ( )
					{
						bool bCBCountdown = false;
						checkBoxCountdown.Dispatcher.Invoke ( DispatcherPriority.Normal, new Action ( ( ) => { bCBCountdown = checkBoxCountdown == null ? false : (bool) checkBoxCountdown.IsChecked; } ) );

						if ( bCBCountdown )
						{
							CountdownPrePlay ( );
						}
						else
						{
							DescretePlay ( );
						}
					} ) );
					oPlayThread.IsBackground = true;
					oPlayThread.Start ( );
				}
				else
				{
					ShowAll ( );
					EndGame ( );
					moWorker.CancelAsync ( );
				}
			}
			catch ( Exception ex )
			{
				Logger.Log ( ex );
			}
		}

		#endregion

		#region Show\Hide + checkbox methods

		//for all
		//ShowAll не нужен - по _simpleOrExtend чекаем че открыть
		private void HideAll ( )
		{
			radioButtonSimple.IsEnabled = false;
			radioButtonExtended.IsEnabled = false;
			SetCountDownDeactivated ( );
			SetSpeedSimpleDeactivated ( );
			SetSimpleTimeDeactivated ( );
			SetSpeedExtendStartDeactivated ( );
			SetSpeedExtendFinishDeactivated ( );
			SetStepPartDeactivated ( );
			SetTimeHitDeactivated ( );
			SetRepeatLoopDeactivated ( );
		}

		private void ShowAll ( )
		{
			radioButtonSimple.IsEnabled = true;
			radioButtonExtended.IsEnabled = true;
			if ( mbSimpleOrExtend )
			{
				radioButtonSimple_Checked ( this, new RoutedEventArgs ( ) );
			}
			else
			{
				radioButtonExtended_Checked ( this, new RoutedEventArgs ( ) );
			}
		}

		//general
		private void ShowSimple ( )
		{
			SetSpeedSimpleActivated ( );
			SetSimpleTimeActivated ( );

			SetSpeedExtendStartDeactivated ( );
			SetSpeedExtendFinishDeactivated ( );
			SetStepPartDeactivated ( );
			SetTimeHitDeactivated ( );
			SetRepeatLoopDeactivated ( );
		}

		private void ShowExtend ( )
		{
			SetSpeedExtendStartActivated ( );
			SetSpeedExtendFinishActivated ( );
			SetStepPartActivated ( );
			SetTimeHitActivated ( );
			SetRepeatLoopActivated ( );

			SetSpeedSimpleDeactivated ( );
			SetSimpleTimeDeactivated ( );
		}

		//for simple
		private void SetCountDownDeactivated ( )
		{
			checkBoxCountdown.IsEnabled = false;
			sliderCountdown.IsEnabled = false;
			labelCountdownUpper.IsEnabled = false;
		}

		private void SetCountDownActivated ( )
		{
			checkBoxCountdown.IsEnabled = true;
			if ( (bool) checkBoxCountdown.IsChecked )
			{
				sliderCountdown.IsEnabled = true;
			}
			labelCountdownUpper.IsEnabled = true;
		}

		private void SetSpeedSimpleDeactivated ( )
		{
			labelSpeedSimple.IsEnabled = false;
			sliderSpeedSimple.IsEnabled = false;
			labelSpeedSimpleUpper.IsEnabled = false;
		}

		private void SetSpeedSimpleActivated ( )
		{
			labelSpeedSimple.IsEnabled = true;
			sliderSpeedSimple.IsEnabled = true;
			labelSpeedSimpleUpper.IsEnabled = true;
		}

		private void SetSimpleTimeDeactivated ( )
		{
			checkBoxSimpleTime.IsEnabled = false;
			sliderSimpleTimer.IsEnabled = false;
			labelSimpleTimerUpper.IsEnabled = false;
		}

		private void SetSimpleTimeActivated ( )
		{
			if ( (bool) checkBoxSimpleTime.IsChecked )
			{
				sliderSimpleTimer.IsEnabled = true;
			}
			checkBoxSimpleTime.IsEnabled = true;
			labelSimpleTimerUpper.IsEnabled = true;
		}

		//for extends
		private void SetSpeedExtendStartDeactivated ( )
		{
			labelSpeedExtendStart.IsEnabled = false;
			sliderSpeedExtendStart.IsEnabled = false;
			labelSpeedExtendStartUpper.IsEnabled = false;
		}

		private void SetSpeedExtendStartActivated ( )
		{
			labelSpeedExtendStart.IsEnabled = true;
			sliderSpeedExtendStart.IsEnabled = true;
			labelSpeedExtendStartUpper.IsEnabled = true;
		}

		private void SetSpeedExtendFinishDeactivated ( )
		{
			labelSpeedExtendFinish.IsEnabled = false;
			sliderSpeedExtendFinish.IsEnabled = false;
			labelSpeedExtendFinishUpper.IsEnabled = false;
		}

		private void SetSpeedExtendFinishActivated ( )
		{
			labelSpeedExtendFinish.IsEnabled = true;
			sliderSpeedExtendFinish.IsEnabled = true;
			labelSpeedExtendFinishUpper.IsEnabled = true;
		}

		private void SetStepPartDeactivated ( )
		{
			buttonStepPart.IsEnabled = false;
			sliderStepPart.IsEnabled = false;
			labelStepPartUpper.IsEnabled = false;
		}

		private void SetStepPartActivated ( )
		{
			buttonStepPart.IsEnabled = true;
			sliderStepPart.IsEnabled = true;
			labelStepPartUpper.IsEnabled = true;
		}

		private void SetTimeHitDeactivated ( )
		{
			buttonTimeHit.IsEnabled = false;
			sliderTimeHit.IsEnabled = false;
			labelTimeHitUpper.IsEnabled = false;
		}

		private void SetTimeHitActivated ( )
		{
			buttonTimeHit.IsEnabled = true;
			sliderTimeHit.IsEnabled = true;
			labelTimeHitUpper.IsEnabled = true;
		}

		private void SetRepeatLoopDeactivated ( )
		{
			checkBoxRepeatLoop.IsEnabled = false;
			buttonRepeatLoop.IsEnabled = false;
			sliderRepeatLoop.IsEnabled = false;
			labelRepeatLoopUpper.IsEnabled = false;
		}

		private void SetRepeatLoopActivated ( )
		{
			checkBoxRepeatLoop.IsEnabled = true;

			if ( (bool) checkBoxRepeatLoop.IsChecked )
			{
				if ( mbRepeatOrLoop )
				{
					sliderRepeatLoop.IsEnabled = true;
					labelRepeatLoopUpper.IsEnabled = true;
				}
				else
				{
					sliderRepeatLoop.IsEnabled = false;
				}
				buttonRepeatLoop.IsEnabled = true;
			}
		}

		//chek-box
		private void checkBoxRepeatLoop_Checked ( object sender, RoutedEventArgs e )
		{
			if ( mbFormLoad )
			{
				SetRepeatLoopActivated ( );
				buttonRepeatLoop.IsEnabled = true;

				//if ((bool)checkBoxRepeatLoop.IsChecked)
				//{
				//    sliderRepeatLoop.IsEnabled = true;
				//}
				//else
				//{
				//    sliderRepeatLoop.IsEnabled = false;
				//}
			}
		}

		private void checkBoxRepeatLoop_Unchecked ( object sender, RoutedEventArgs e )
		{
			if ( mbFormLoad )
			{
				SetRepeatLoopDeactivated ( );
				//костыль ибо предыдущий метод деактивирует чек-бокс
				checkBoxRepeatLoop.IsEnabled = true;
			}
		}

		private void checkBoxCountdown_Checked ( object sender, RoutedEventArgs e )
		{
			if ( mbFormLoad )
			{
				sliderCountdown.IsEnabled = true;
				labelCountdownUpper.IsEnabled = true;
			}
		}

		private void checkBoxCountdown_Unchecked ( object sender, RoutedEventArgs e )
		{
			if ( mbFormLoad )
			{
				sliderCountdown.IsEnabled = false;
				labelCountdownUpper.IsEnabled = false;
			}
		}

		private void checkBoxSimpleTime_Checked ( object sender, RoutedEventArgs e )
		{
			if ( mbFormLoad )
			{
				sliderSimpleTimer.IsEnabled = true;
				labelSimpleTimerUpper.IsEnabled = true;
			}
		}

		private void checkBoxSimpleTime_Unchecked ( object sender, RoutedEventArgs e )
		{
			if ( mbFormLoad )
			{
				sliderSimpleTimer.IsEnabled = false;
				labelSimpleTimerUpper.IsEnabled = false;
			}
		}

		#endregion

		#region Кнопки меню

		private void MenuLoad_Click ( object sender, RoutedEventArgs e )
		{
			try
			{
				OpenFileDialog dlg = new OpenFileDialog ( );
				dlg.FileName = "preset";
				dlg.DefaultExt = ".prob";
				dlg.Filter = "Metronom preset (.prob)|*.prob";
				bool? result = dlg.ShowDialog ( );
				if ( result == true )
				{
					msFileNameToSavePreset = dlg.FileName;
					moPresetObj.LoadPreset ( msFileNameToSavePreset );
				}
			}
			catch ( Exception ex )
			{
				Logger.Log ( ex );
			}
		}

		private void MenuSave_Click ( object sender, RoutedEventArgs e )
		{
			moPresetObj = new PresetObj ( this );
			moPresetObj.SavePreset ( msFileNameToSavePreset );
		}

		private void MenuSaveAs_Click ( object sender, RoutedEventArgs e )
		{
			try
			{
				SaveFileDialog dlg = new SaveFileDialog ( );
				dlg.FileName = "preset";
				dlg.DefaultExt = ".prob";
				dlg.Filter = "Metronom preset (.prob)|*.prob";
				Nullable<bool> result = dlg.ShowDialog ( );
				if ( result == true )
				{
					moPresetObj = new PresetObj ( this );
					msFileNameToSavePreset = dlg.FileName;
					moPresetObj.SavePreset ( msFileNameToSavePreset );
				}
			}
			catch ( Exception ex )
			{
				Logger.Log ( ex );
			}
		}

		private void MenuExit_Click ( object sender, RoutedEventArgs e )
		{
			try
			{
				Close ( );
			}
			catch ( Exception ex )
			{
				Logger.Log ( ex );
			}

		}

		private void MenuHelp_Click ( object sender, RoutedEventArgs e )
		{
			try
			{
				About oAboutForm = new About ( );
				oAboutForm.ShowDialog ( );
			}
			catch ( Exception ex )
			{
				Logger.Log ( ex );
			}
		}

		#endregion

		#region Slider-обработка

		private void sliderCountdown_ValueChanged ( object sender, RoutedPropertyChangedEventArgs<double> e )
		{
			if ( mbFormLoad )
			{
				labelCountdownUpper.Content = Math.Round ( sliderCountdown.Value );
				sliderCountdown.Value = Math.Round ( sliderCountdown.Value );
			}
		}

		private void sliderSpeedSimple_ValueChanged ( object sender, RoutedPropertyChangedEventArgs<double> e )
		{
			if ( mbFormLoad )
			{
				labelSpeedSimpleUpper.Content = Math.Round ( sliderSpeedSimple.Value );
				sliderSpeedSimple.Value = Math.Round ( sliderSpeedSimple.Value );
			}
		}

		private void sliderSimpleTimer_ValueChanged ( object sender, RoutedPropertyChangedEventArgs<double> e )
		{
			if ( mbFormLoad )
			{
				labelSimpleTimerUpper.Content = Math.Round ( sliderSimpleTimer.Value );
				sliderSimpleTimer.Value = Math.Round ( sliderSimpleTimer.Value );
			}
		}

		private void sliderSpeedExtendStart_ValueChanged ( object sender, RoutedPropertyChangedEventArgs<double> e )
		{
			if ( mbFormLoad )
			{
				labelSpeedExtendStartUpper.Content = Math.Round ( sliderSpeedExtendStart.Value );
				sliderSpeedExtendStart.Value = Math.Round ( sliderSpeedExtendStart.Value );
			}
		}

		private void sliderSpeedExtendFinish_ValueChanged ( object sender, RoutedPropertyChangedEventArgs<double> e )
		{
			if ( mbFormLoad )
			{
				labelSpeedExtendFinishUpper.Content = Math.Round ( sliderSpeedExtendFinish.Value );
				sliderSpeedExtendFinish.Value = Math.Round ( sliderSpeedExtendFinish.Value );
			}
		}

		private void sliderStepPart_ValueChanged ( object sender, RoutedPropertyChangedEventArgs<double> e )
		{
			if ( mbFormLoad )
			{
				labelStepPartUpper.Content = Math.Round ( sliderStepPart.Value );
				sliderStepPart.Value = Math.Round ( sliderStepPart.Value );
			}
		}

		private void sliderTimeHit_ValueChanged ( object sender, RoutedPropertyChangedEventArgs<double> e )
		{
			if ( mbFormLoad )
			{
				labelTimeHitUpper.Content = Math.Round ( sliderTimeHit.Value );
				sliderTimeHit.Value = Math.Round ( sliderTimeHit.Value );
			}
		}

		private void sliderRepeatLoop_ValueChanged ( object sender, RoutedPropertyChangedEventArgs<double> e )
		{
			if ( mbFormLoad )
			{
				labelRepeatLoopUpper.Content = Math.Round ( sliderRepeatLoop.Value );
				sliderRepeatLoop.Value = Math.Round ( sliderRepeatLoop.Value );
			}
		}

		#endregion

		#region Public UI Methods

		public void ReverseRadioButton ( )
		{
			if ( mbSimpleOrExtend )
			{
				radioButtonExtended.IsChecked = true;
				radioButtonSimple.IsChecked = false;
				mbSimpleOrExtend = false;
			}
			else
			{
				radioButtonSimple.IsChecked = true;
				radioButtonExtended.IsChecked = false;
				mbSimpleOrExtend = true;
			}
		}

		#endregion

		#region Переключение тем

		private void ClickOnStyle ( object sender, RoutedEventArgs e )
		{
			try
			{
				if ( StylesDropdown.HasItems )
				{
					foreach ( MenuItem oItem in StylesDropdown.Items )
					{
						oItem.IsChecked = false;
					}

					MenuItem oCheckedStyle = e.OriginalSource as MenuItem;
					oCheckedStyle.IsChecked = true;
					this.Resources.MergedDictionaries.Add ( new ResourceDictionary ( ) { Source = new Uri ( String.Format ( "Themes/{0}.xaml", oCheckedStyle.Header ), UriKind.RelativeOrAbsolute ) } );
					ReverseRadioButton ( );
				}
			}
			catch ( Exception ex )
			{
				Logger.Log ( "Can't find theme in Themes folder. Original Message: " + ex );
			}
		}

		private void ChangeLanguageClick ( object sender, RoutedEventArgs e )
		{
			if ( LanguagesDropdown.HasItems )
			{
				foreach ( MenuItem oItem in LanguagesDropdown.Items )
				{
					oItem.IsChecked = false;
				}
				MenuItem oChoosedLanguage = e.OriginalSource as MenuItem;
				oChoosedLanguage.IsChecked = true;

				// translations...
			}
		}

		#endregion

	}
}
